package com.deke.home.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.TbHeadNav;
import com.deke.entity.TbZone;
import com.deke.home.dao.TbHeadNavDao;
import com.deke.home.service.TbHeadNavService;
import com.deke.home.vo.TbHeadNavVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/6 13:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbHeadNavServiceImpl implements TbHeadNavService {

    @Resource
    private TbHeadNavDao tbHeadNavDao;


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 分页查询
     * @create time: 2022/12/6 13:19
     * @param current
     * @param pageSize
     * @param tbHeadNavVo
     * @return com.deke.vo.Result<com.baomidou.mybatisplus.core.metadata.IPage<com.deke.entity.TbHeadNav>>
     */
    @Override
    public Result<IPage<TbHeadNav>> queryByConditionPage(Integer current, Integer pageSize, TbHeadNavVo tbHeadNavVo) {
        IPage<TbHeadNav> page = new Page(current,pageSize);
        QueryWrapper<TbHeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", "0");
        if (!StringUtils.isEmpty(tbHeadNavVo.getHeadId())){
            wrapper.like("head_id",tbHeadNavVo.getHeadId());
        }
        if (StringUtils.hasText(tbHeadNavVo.getHeadName())){
            wrapper.like("head_name", tbHeadNavVo.getHeadName());
        }
        if (StringUtils.hasText(tbHeadNavVo.getIsDisable())){
            wrapper.eq("is_disable", tbHeadNavVo.getIsDisable());
        }
        IPage<TbHeadNav> iPage = tbHeadNavDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询成功",iPage);
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 删除操作
     * @create time: 2022/12/6 13:35
     * @param headId
     * @return com.deke.vo.Result
     */
    @Override
    public void deleteHeadNav(TbHeadNav tbHeadNav, Integer headId) {
        UpdateWrapper<TbHeadNav> wrapper = new UpdateWrapper<>();
        wrapper.eq("head_id", headId);
        tbHeadNavDao.update(tbHeadNav,wrapper);
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 添加操作
     * @create time: 2022/12/6 17:18
     * @param tbHeadNav
     * @return com.deke.vo.Result
     */
    @Override
    public Result addHeadNav(TbHeadNav tbHeadNav) {
        int insert = tbHeadNavDao.insert(tbHeadNav);
        if (ObjectUtil.isNotEmpty(insert)){
            return new Result(2000, "添加成功");
        }else {
            return new Result(5000, "添加失败");
        }
    }

    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 修改操作
     * @create time: 2022/12/6 17:35
     * @param tbHeadNav
     * @return com.deke.vo.Result
     */
    @Override
    public Result updateHeadNav(TbHeadNav tbHeadNav) {
        UpdateWrapper<TbHeadNav> wrapper = new UpdateWrapper<>();
        wrapper.eq("head_id", tbHeadNav.getHeadId());
        int update = tbHeadNavDao.update(tbHeadNav, wrapper);
        if (ObjectUtil.isNotEmpty(update)){
            return new Result(2000, "修改成功");
        }else {
            return new Result(5000, "修改失败");
        }
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 改变状态
     * @create time: 2022/12/6 17:18
     * @param headId
     * @param isDisable
     * @param tbHeadNav
     * @return com.deke.vo.Result
     */
    @Override
    public Result changeStatus(Integer headId, String isDisable) {
        UpdateWrapper<TbHeadNav> wrapper = new UpdateWrapper<>();
        wrapper.eq("head_id", headId);
        TbHeadNav tbHeadNav = new TbHeadNav();
        tbHeadNav.setIsDisable(isDisable);
        int update = tbHeadNavDao.update(tbHeadNav, wrapper);
        if (ObjectUtil.isNotEmpty(update)){
            return new Result(2000, "状态改变成功");
        }
        return new Result(5000,"状态改变失败");
    }
}